# HG changeset patch # User Mads Kiilerich # Date 1721665203 -7200 # Mon Jul 22 18:20:03 2024 +0200 # Node ID e5498d633f47c95891aa22ec354a6b19e3bf5260 # Parent 3b18b545b9bf7af5ab97805121fa52b31a319d53 utils: fix resourceutil use of deprecated importlib.resources Some importlib functionality was deprecated in 3.11 . The documentation on https://docs.python.org/3.12/library/importlib.resources.html recommends using the new .files() API that was introduced in 3.9. diff --git a/mercurial/utils/resourceutil.py b/mercurial/utils/resourceutil.py --- a/mercurial/utils/resourceutil.py +++ b/mercurial/utils/resourceutil.py @@ -66,7 +66,7 @@ try: from importlib import resources # pytype: disable=import-error # Force loading of the resources module - if hasattr(resources, 'files'): + if hasattr(resources, 'files'): # Introduced in Python 3.9 resources.files # pytype: disable=module-attr else: resources.open_binary # pytype: disable=module-attr @@ -115,12 +115,20 @@ else: ) def is_resource(package: bytes, name: bytes) -> bool: - return resources.is_resource( # pytype: disable=module-attr - pycompat.sysstr(package), encoding.strfromlocal(name) - ) + if hasattr(resources, 'files'): # Introduced in Python 3.9 + return resources.files(pycompat.sysstr(package)).joinpath(encoding.strfromlocal(name)).is_file() + else: + return resources.is_resource( # pytype: disable=module-attr + pycompat.sysstr(package), encoding.strfromlocal(name) + ) def contents(package: bytes) -> "Iterator[bytes]": - # pytype: disable=module-attr - for r in resources.contents(pycompat.sysstr(package)): - # pytype: enable=module-attr - yield encoding.strtolocal(r) + if hasattr(resources, 'files'): # Introduced in Python 3.9 + for path in resources.files(pycompat.sysstr(package)).iterdir(): + if path.is_file(): + yield encoding.strtolocal(path.name) + else: + # pytype: disable=module-attr + for r in resources.contents(pycompat.sysstr(package)): + # pytype: enable=module-attr + yield encoding.strtolocal(r) _______________________________________________ Mercurial-devel mailing list Mercurial-devel@lists.mercurial-scm.org https://lists.mercurial-scm.org/mailman/listinfo/mercurial-devel